@extends('admin.layout.app')

@section('title', '内容分类')
@section('style')
    <style>
        table .layui-table-cell{
            height:auto;
            overflow:visible;
            text-overflow:inherit;
            white-space:normal;
        }
    </style>
@endsection
@section('content')
    <div class="layui-col-lg12">
        <div class="layui-card">
            <div class="layui-card-header">
                内容分类列表
            </div>
            <div class="layui-card-body">
                <form class="layui-form" id="search-form" action="" onsubmit="return false;">
                    <div class="tool-bar layui-form-item">
                        <div class="layui-inline">
                            <select name="type" lay-filter="aihao">
                                <option value="">请选择</option>
                                @if(isset($typeMap))
                                    @foreach ($typeMap as $key => $val)
                                        @if(isset($row['type']) && $row['type'] == $key)
                                            <option value="{{$key}}" selected>{{$val}}</option>
                                        @else
                                            <option value="{{$key}}">{{$val}}</option>
                                        @endif
                                    @endforeach
                                @endif
                            </select>
                        </div>

                        <div class="layui-inline">
                            <div class="layui-btn-group">
                                <button class="layui-btn" data-type="reload" title="搜索">
                                    <i class="layui-icon layui-icon-search"></i>
                                </button>
                                <a class="layui-btn" href="/message/template/edit/0" title="添加">
                                    <i class="layui-icon layui-icon-add-1"></i>
                                </a>
                            </div>
                        </div>
                    </div>
                </form>
                <table class="layui-hide" id="lst" lay-filter="tb"></table>

                <script type="text/html" id="toolbar">
                    <div class="layui-btn-group">
                        <a class="layui-btn layui-btn-xs" lay-event="edit" data-type="edit" href="@broute('message.template.edit', ['@{{d.id}}'])">
                            <i class="layui-icon layui-icon-edit"></i>
                        </a>
                    </div>
                </script>
                <div class="tool-bar layui-form-item layui-btn-group">
                    <a class="layui-btn layui-btn-danger" data-type="delete">
                        <i class="layui-icon"></i>批量删除
                    </a>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('script')
    <script>
        layui.use('table', function () {
            var table = layui.table;
            //方法级渲染
            table.render({
                elem: '#lst'
                , url: "@broute('message.template.index')"
                , cols: [[
                    {checkbox: true}
                    , {field: 'type_name', title: '模板类别', width: 150}
                    , {field: 'title', title: '模板标题', width: 250}
                    , {field: 'content', title: '模板内容'}
                    , {fixed: 'right', width: 200, align: 'center', toolbar: '#toolbar'}
                ]]
                , id: 'table'
                , page: true
                , limit: 20
                , loading: true
            });
            var $ = layui.$, active = {
                reload: function () {
                    // 执行重载
                    table.reload('table', {
                        page: {curr: 1}
                        , where: $('#search-form').serializeJSON()
                    });
                },
                delete: function () {
                    var checkStatus = table.checkStatus('table')
                        , checked = checkStatus.data
                        , id = [];
                    $.map(checked, function (k, v) {
                        id.push(k.id);
                    });
                    var data = {
                        id: id,
                    };
                    $.ajax({
                        url: "@broute('message.template.delete')",
                        type: 'post',
                        data: data,
                        dataType: 'json',
                        success: function (data) {
                            if (!data.status) {
                                layer.msg(data.message, function () {
                                });
                                return;
                            }
                            layer.msg(data.message);
                            setTimeout(function () {
                                // 刷新列表
                                active['reload'].call(this);
                            }, 1000);
                        },
                        error: function (jqXHR, textStatus) {
                            // 友好提示
                            layer.msg(jqXHR.responseJSON.message || jqXHR.responseText, function () {
                            });
                        }
                    });
                }
            };

            $('.tool-bar .layui-btn, #toolbar a[data-type]').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>
@endsection
